package com.file.parser;

import com.file.bo.ResponseData;
import com.file.bo.mail.CGB;
import com.file.bo.mail.CGBTran;
import com.file.constant.EfpConstants;
import com.file.constant.ErrorCode;
import com.file.parser.traprange.PDFTableExtractor;
import com.file.util.JsonUtils;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class CGBPdfParser extends BaseDecryptPdfParser {

	public ResponseData<String> parseCGBPdfToJson(String filePath, String pdfPassword) {
		log.info("parseCGBPdfToJson started, pdfPassword:{}", pdfPassword);
		String json = null;

		try {
			CGB cgb = parseCGBPdf(filePath, pdfPassword);
			json = JsonUtils.convertObjectToJson(cgb);
		} catch (Exception e) {
			log.error("[OnError]parseCGBPdfToJson failed, pdfPassword:{}", pdfPassword, e);
			return new ResponseData<String>(json, ErrorCode.FILE_PARSE_EXCEPTION.getCode(),
					ErrorCode.FILE_PARSE_EXCEPTION.getMsg());
		}

		log.info("parseCGBPdfToJson completed, pdfPassword:{}", pdfPassword);
		return new ResponseData<String>(json, ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg());
	}

	private CGB parseCGBPdf(String filePath, String pdfPassword) {
		CGB cgb = parseCGBHeader(filePath, pdfPassword);

		List<CGBTran> cgbTrans = parseCgbTrans(filePath, pdfPassword);

		cgb.setCgbTrans(cgbTrans);

		return cgb;
	}

	private CGB parseCGBHeader(String filePath, String pdfPassword) {
		CGB cgb = new CGB();
		String pdfHeaderText = parsePdfHeaderText(filePath, pdfPassword);
		// log.info(pdfHeaderText);

		String accountNo = pdfHeaderText
				.substring(pdfHeaderText.indexOf("个人账户交易流水证明账号:") + 13, pdfHeaderText.indexOf("户名: ")).trim();
		String accountName = pdfHeaderText.substring(pdfHeaderText.indexOf("户名: ") + 3, pdfHeaderText.indexOf("起止日期: "))
				.trim();
		String inquiryPeriod = pdfHeaderText
				.substring(pdfHeaderText.indexOf("起止日期: ") + 5, pdfHeaderText.indexOf("交易时间 币种")).trim();

		cgb.setAccountNo(accountNo);
		cgb.setAccountName(accountName);
		cgb.setInquiryPeriod(inquiryPeriod);

		return cgb;
	}

	private List<CGBTran> parseCgbTrans(String filePath, String pdfPassword) {
		List<CGBTran> cgbTrans = new ArrayList<>();

		String transText = parseTransToText(filePath, pdfPassword);
		if (Strings.isNullOrEmpty(transText)) {
			return cgbTrans;
		}

		List<List<String>> tranFieldsList = parseTransTextToTranFieldsList(transText);

		for (List<String> strings : tranFieldsList) {
			// log.info(strings.toString());
			CGBTran cgbTran = new CGBTran();
			cgbTran.setTranDate(strings.get(0));
			cgbTran.setCurrency(strings.get(1));
			cgbTran.setTranAmt(strings.get(2));
			cgbTran.setAccountBalance(strings.get(3));
			cgbTran.setCounterPartyAccountName(strings.get(4));
			cgbTran.setConterPartyAccountNo(strings.get(5));
			cgbTran.setSummary(strings.get(6));
			cgbTrans.add(cgbTran);
		}

		return cgbTrans;
	}

	private String parseTransToText(String filePath, String pdfPassword) {
		PDFTableExtractor extractor = (new PDFTableExtractor()).setSource(filePath, pdfPassword);
		int pdfPageNumber = getPdfPageNumber(filePath, pdfPassword);

		for (int k = 0; k < pdfPageNumber; k++) {
			int[] skipLinesIndexes = new int[EfpConstants.CGB_PDF_HEADER_LINE_NUMBER
					+ EfpConstants.CGB_PDF_FOOTER_LINE_NUMBER];
			for (int i = 0; i < EfpConstants.CGB_PDF_HEADER_LINE_NUMBER; i++) {
				skipLinesIndexes[i] = i;
			}
			for (int j = 0; j < EfpConstants.CGB_PDF_FOOTER_LINE_NUMBER; j++) {
				skipLinesIndexes[EfpConstants.CGB_PDF_HEADER_LINE_NUMBER + j] = -(j + 1);
			}
			extractor.exceptLine(k, skipLinesIndexes);
		}

		return extractPdfToText(extractor);
	}

	public static void main(String[] args) {
		CGBPdfParser ccbPdfParser = new CGBPdfParser();
		CGB cgb = ccbPdfParser.parseCGBPdf("E:\\data\\files\\CGB\\广发流水（9612722409）.pdf", "9612722409");

		String json = JsonUtils.convertObjectToJson(cgb);
		System.out.println(json);
	}

}
